<?php

namespace app\service;

use think\facade\Db;

use Exception;
use app\model\SaleModel;

class ReportService
{
    public function list($params)
    {
        //按出发日期查询
        if (!empty($params['start_date']) && !empty($params['end_date'])) {
            if (!empty($params['start_date'])) {
                $where[] = ['start_date', '>= time', $params['start_date']];
            }
            if (!empty($params['end_date'])) {
                $where[] = ['start_date', '<= time', $params['end_date']];
            }
        }
        $where[] = ['type', '=', $params['type']];

        $data = SaleModel::where($where)->order('id', 'desc')->select();
        $resprt_total_price = 0;
        $resprt_receipt = 0;
        $resprt_no_receipt = 0;
        $resprt_total_cost_price = 0;
        $resprt_payment = 0;
        $resprt_not_payment = 0;
        $resprt_profit = 0;
        foreach ($data as &$v) {
            $incomes = Db::name('sale_income')->where(['type' => $v['type'], 'pid' => $v['id']])->select();
            $total_price = 0;
            $receipt = 0;
            foreach ($incomes as $income) {
                $total_price += $income['sell_price'] * $income['num'];
                $receipt += Db::name('receipt')->where(['type' => $income['type'], 'income_id' => $income['id']])->sum('money');
            }
            $resprt_total_price += $total_price;
            $resprt_receipt += $receipt;
            $resprt_no_receipt += $total_price - $receipt;

            $outlays = Db::name('sale_outlay')->where(['type' => $v['type'], 'pid' => $v['id']])->select();
            $total_cost_price = 0;
            $payment = 0;
            foreach ($outlays as $outlay) {
                $total_cost_price += $outlay['cost_price'] * $outlay['num'];
                $payment += Db::name('payment')->where(['type' => $outlay['type'], 'outlay_id' => $outlay['id']])->sum('money');
            }
            $resprt_total_cost_price += $total_cost_price;
            $resprt_payment += $payment;
            $resprt_not_payment += $total_cost_price - $payment;

            $resprt_profit += $total_price - $total_cost_price;
        }
        $title = '';
        if ($params['type'] == 4) {
            $title = "项目";
        } elseif ($params['type'] == 5) {
            $title = "维修维护";
        } elseif ($params['type'] == 6) {
            $title = "自用";
        }

        $report = [[
            'title' => $title,
            'total_sell_price' => $resprt_total_price,
            'received' => $resprt_receipt,
            'no_received' => $resprt_no_receipt,
            'total_cost_price' => $resprt_total_cost_price,
            'payment' => $resprt_payment,
            'no_payment' => $resprt_not_payment,
            'profit' => $resprt_profit
        ]];
        return $report;
    }
    public function summary($params)
    {
        //按出发日期查询
        if (!empty($params['start_date']) && !empty($params['end_date'])) {
            if (!empty($params['start_date'])) {
                $where[] = ['start_date', '>= time', $params['start_date']];
            }
            if (!empty($params['end_date'])) {
                $where[] = ['start_date', '<= time', $params['end_date']];
            }
        }
       

        $data = SaleModel::where($where)->order('id', 'desc')->select();
        $resprt_total_price = 0;
        $resprt_receipt = 0;
        $resprt_no_receipt = 0;
        $resprt_total_cost_price = 0;
        $resprt_payment = 0;
        $resprt_not_payment = 0;
        $resprt_profit = 0;
        foreach ($data as &$v) {
            $incomes = Db::name('sale_income')->where(['type' => $v['type'], 'pid' => $v['id']])->select();
            $total_price = 0;
            $receipt = 0;
            foreach ($incomes as $income) {
                $total_price += $income['sell_price'] * $income['num'];
                $receipt += Db::name('receipt')->where(['type' => $income['type'], 'income_id' => $income['id']])->sum('money');
            }
            $resprt_total_price += $total_price;
            $resprt_receipt += $receipt;
            $resprt_no_receipt += $total_price - $receipt;

            $outlays = Db::name('sale_outlay')->where(['type' => $v['type'], 'pid' => $v['id']])->select();
            $total_cost_price = 0;
            $payment = 0;
            foreach ($outlays as $outlay) {
                $total_cost_price += $outlay['cost_price'] * $outlay['num'];
                $payment += Db::name('payment')->where(['type' => $outlay['type'], 'outlay_id' => $outlay['id']])->sum('money');
            }
            $resprt_total_cost_price += $total_cost_price;
            $resprt_payment += $payment;
            $resprt_not_payment += $total_cost_price - $payment;

            $resprt_profit += $total_price - $total_cost_price;
        }
        
        $report = [[           
            'total_sell_price' => $resprt_total_price,
            'received' => $resprt_receipt,
            'no_received' => $resprt_no_receipt,
            'total_cost_price' => $resprt_total_cost_price,
            'payment' => $resprt_payment,
            'no_payment' => $resprt_not_payment,
            'profit' => $resprt_profit
        ]];
        return $report;
    }
}
